// pages/customerManage/customerManage.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    /*  */
    userInfo_share:{},
    /* tab数据 */
    activeIndex:"1",
    tabDatas:[
      {
        name:'今天',
        type:'1'
      },
      {
        name:'昨天',
        type:'2'
      },
      {
        name:'本周',
        type:'3'
      },
      {
        name:'本月',
        type:'4'
      },
      {
        name:'更多',
        type:'5'
      }
    ],
    /* tabContent数据 */
    contentIndex:'0',  //内容标记
    tabContentData:[
      {
        name:'客户列表',
        id:'0',
        emptyWord:'您的企业还没有客户哦，赶快让大家分享名片出去获客吧！',
        showListEmpty:false,
        data:[],   //列表数据
        reqFnName:'getClients',  //获取列表数据方法名
        page:1,  //页码
        pageSize:10, //每页条数
      }
    ],
    /*  */
    clientArgs:{
      corp_userid:"",
      page:1,  //页码
      pageSize:10, //每页条数
      type:"1"  //1、今天，2、昨天，3、本周，4、本月，5、更多
    },
    /*  */
    showTimePop:false
  },
  /*  */
  goToIndex(){
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  /* 跳转至客户详情 */
  goToVisitDetail:function(e){
    let { info } = e.currentTarget.dataset;
    //
    console.log(info);
    app.globalData.fromCustomerManageToVisitDetail = true;
    wx.navigateTo({
      url: '/pages/customerDetail/customerDetail',
      success:res=>{
        res.eventChannel.emit('fromCustomerManageToVisitDetail',{
          info:info
        });
      }
    })
  },
  /* 获取客户数据 */
  getClients(){
    //
    wx.showLoading({
      title:'获取客户数据',
      mask:true
    });
    //
    this.setData({
      'clientArgs.page':this.data.tabContentData[this.data.contentIndex*1].page,
      'clientArgs.corp_userid':this.data.userInfo_share.corpUser.userid
    });
    //
    return app.globalData.$http.call(this,{
      api:"/getClients",
      data:this.data.clientArgs
    }).then(res=>{
      console.log(res)
      wx.hideLoading();
      if(res.data.code=='1'){
        if(res.data.body.data.length){
          let tmpData = this.data.tabContentData[this.data.contentIndex*1].data;
          let page = this.data.tabContentData[this.data.contentIndex*1].page;
          res.data.body.data.forEach(item=>{
            tmpData.push(item);
          });
          page++;
          //
          this.setData({
            [`tabContentData[${this.data.contentIndex*1}].page`]:page,
            [`tabContentData[${this.data.contentIndex*1}].data`]:tmpData,
            [`tabContentData[${this.data.contentIndex*1}].showListEmpty`]:false
          });
        }
        //列表为空
        if(!res.data.body.data.length&&!this.data.tabContentData[this.data.contentIndex*1].data.length){
          //
          this.setData({
            [`tabContentData[${this.data.contentIndex*1}].showListEmpty`]:true
          });
        }
        return Promise.resolve()
      }else{
        wx.showToast({
          icon:'none',
          title:'获取客户列表数据失败'
        });
      }
    }).catch(err=>{
      wx.hideLoading();
      wx.showToast({
        icon:'none',
        title:'获取客户列表数据失败'
      });
    })
  },
  /* 切换客户数据 */
  tabCustomerData:function(e){
    console.log(e);
    let type = this.data.tabDatas[e.detail.index].type;
    this.setData({
      "clientArgs.type":type
    })
    //
    let tmpData = this.data.tabContentData[this.data.contentIndex*1].data;
    tmpData.splice(0,tmpData.length);
    let page = this.data.tabContentData[this.data.contentIndex*1].data.page;
    page = 1;
    this.setData({
      [`tabContentData[${this.data.contentIndex*1}].page`]:page,
      [`tabContentData[${this.data.contentIndex*1}].data`]:tmpData
    });
    //显示更多数据弹层
    if(type=='5'){
      this.setData({
        showTimePop:true
      });
    }else{
      this.setData({
        'clientArgs.start_time':'',  //开始时间
        'clientArgs.end_time':'',
      });
      //
      this[this.data.tabContentData[this.data.contentIndex*1].reqFnName]();
    }
  },
  /* 更多弹层的时间选择 */
  changeMoreTime(e){
    console.log(e);
    /* 
      e.detail.endTime: "2020-07-16"
      e.detail.startTime: "2020-07-11"
    */
    //
    this.setData({
      'clientArgs.start_time':e.detail.startTime,  
      'clientArgs.end_time':e.detail.endTime,
    });
    //
    this[this.data.tabContentData[this.data.contentIndex*1].reqFnName]();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //获取用户信息
    this.setData({
      userInfo_share:app.globalData.get_userInfo()
    });
    //
    let eventChannel = this.getOpenerEventChannel()
    //
    if(app.globalData.fromEmployeeToCustomerManage){
      app.globalData.fromEmployeeToCustomerManage = false;
      eventChannel.on("fromEmployeeToCustomerManage",data=>{
        console.log(data);
        wx.setNavigationBarTitle({
          title: `${data.name}`,
        })
      });
    }
    //打开页面--默认获取客户列表数据
    if(!this.data.tabContentData[this.data.contentIndex*1].data.length){
      typeof this[this.data.tabContentData[this.data.contentIndex*1].reqFnName]==='function'&&this[this.data.tabContentData[this.data.contentIndex*1].reqFnName]();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /* 下拉刷新 */
  pageContentRefresh:function(){
    //
    this.setData({
      [`tabContentData[${this.data.contentIndex*1}].page`]:1,
      [`tabContentData[${this.data.contentIndex*1}].data`]:[],
    });
    //
    this[this.data.tabContentData[this.data.contentIndex*1].reqFnName]().then(()=>{
      wx.stopPullDownRefresh({
        complete: (res) => {},
      });
    }).catch(()=>{
      wx.stopPullDownRefresh({
        complete: (res) => {},
      });
    });
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.pageContentRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this[this.data.tabContentData[this.data.contentIndex*1].reqFnName]();
  }
})